<?php

declare(strict_types=1);

namespace App\Controller\m1;

use Throwable;
use App\Helper\Log;
use App\Helper\Util;
use App\Helper\Common;
use App\Helper\Verify;
use Hyperf\DbConnection\Db;
use App\Constants\ReturnCode;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\Manager\AuthMiddleware;
use App\Service\ShowService;
use App\Service\PlatformService;
use App\Service\PlaceService;

#[Controller]
#[Middleware(AuthMiddleware::class)]
class ShowController extends AbstractController
{
    #[Inject]
    protected ShowService $showServ;
    #[Inject]
    protected PlatformService $platServ;
    #[Inject]
    protected PlaceService $placeServ;
    #演出系列列表
    #[RequestMapping(path: "list", methods: "post")]
    public function list()
    {
        $person_id = $this->request->input('person_id', 0);
        $name = $this->request->input('name', '');
        $status = $this->request->input('status', -1);
        $currentPage = $this->request->input('currentPage', 1);
        $pageSize = $this->request->input('pageSize', 15);
        $where = [['is_del', '=', 0]];
        if (!empty($person_id)) {
            $where[] = ['person_id', '=', $person_id];
        }
        if (!empty($name)) {
            $where[] = ['name', 'like', "%".trim($name)."%"];
        }
        if ($status!=-1) {
            $where[] = ['status', '=', $status];
        }
        $offset = ($currentPage - 1) * $pageSize;
        $columns = [
            'id',
            'person_id',
            'name',
            'image',
            'sort',
            'status',
            'created_at',
            'updated_at',
        ];
        $query = Db::table('vote_show')
            ->select($columns)
            ->where($where);
        $total = $query->count();
        $datas = $query->offset($offset)
            ->limit($pageSize)
            ->orderBy('sort', 'desc')
            ->orderBy('created_at', 'desc')
            ->get()
            ->toArray();
        $return_list = [];
        if(!empty($datas)){
            $statusArr = ["1"=>'显示', "0"=>'隐藏'];
            foreach ($datas as $data) {
                $person_name = Db::table('vote_person')
                    ->where(['id'=>$data['person_id']])
                    ->value("name");
                $status_ch = !empty($statusArr[$data['status']])?$statusArr[$data['status']]:"-";
                $return_list[] = [
                    'id'=>$data['id'],
                    'person_id'=>$data['person_id'],
                    'person_name'=>$person_name,
                    'name'=>$data['name'],
                    'image'=>$data['image'],
                    'sort'=>$data['sort'],
                    'status'=>$data['status'],
                    'status_ch'=>$status_ch,
                    'created_at'=>date("Y-m-d H:i:s", $data['created_at']),
                    'updated_at'=>date("Y-m-d H:i:s", $data['updated_at'])
                ];
            }
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', ['total'=>$total, 'list'=>$return_list]);
    }
    #演出系列详情
    #[RequestMapping(path: "detail", methods: "post")]
    public function detail()
    {
        $id = $this->request->input('id', 0);
        if(empty($id)){
            return $this->fmt_response(ReturnCode::para_miss,'ID不能为空');
        }
        $columns = [
            'id',
            'person_id',
            'name',
            'image',
            'sort',
            'status'
        ];
        $data = Db::table('vote_show')
            ->select($columns)
            ->where(['id'=>$id])
            ->first();
        $return_data = [];
        if(!empty($data)){
            $person_name = Db::table('vote_person')
                ->where(['id'=>$data['person_id']])
                ->value("name");
            $statusArr = ["1"=>'显示', "0"=>'隐藏'];
            $status_ch = !empty($statusArr[$data['status']])?$statusArr[$data['status']]:"-";
            $return_data = [
                'id'=>$data['id'],
                'person_id'=>$data['person_id'],
                'person_name'=>$person_name,
                'name'=>$data['name'],
                'image'=>$data['image'],
                'sort'=>$data['sort'],
                'status'=>$data['status'],
                'status_ch'=>$status_ch
            ];
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $return_data);
    }
    #演出系列保存
    #[RequestMapping(path: "save", methods: "post")]
    public function save()
    {
        $id = $this->request->input('id', 0);
        $person_id = $this->request->input('person_id', 0);
        $name = $this->request->input('name', '');
        $image = $this->request->input('image', '');
        $sort = $this->request->input('sort', 0);
        $status = $this->request->input('status', -1);
        if(empty($person_id)){
            return $this->fmt_response(ReturnCode::para_miss,'没有关联人物');
        }
        if(empty($name)){
            return $this->fmt_response(ReturnCode::para_miss,'名称不能为空');
        }
        if(empty($image)){
            return $this->fmt_response(ReturnCode::para_miss,'图片不能为空');
        }
        if($status==-1){
            return $this->fmt_response(ReturnCode::para_miss,'状态错误');
        }
        $params = [
            'id'=>$id,
            'person_id'=>$person_id,
            'name'=>$name,
            'image'=>$image,
            'sort'=>$sort,
            'status'=>$status
        ];
        $res = $this->showServ->showSave($params);
        if(!$res){
            return $this->fmt_response(ReturnCode::deal_false,'失败');
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #演出系列删除
    #[RequestMapping(path: "del", methods: "post")]
    public function del()
    {
        $id = $this->request->input('id', 0);
        if(empty($id)){
            return $this->fmt_response(ReturnCode::para_miss,'ID不能为空');
        }
        $res = Db::table('vote_show')
            ->where(['id'=>$id])
            ->update(['is_del'=>1]);
        if(!$res){
            return $this->fmt_response(ReturnCode::deal_false,'失败');
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #演出系列搜索
    #[RequestMapping(path: "search", methods: "post")]
    public function search()
    {
        $name = $this->request->input('name', '');
        if(empty($name)){
            return $this->fmt_response(ReturnCode::deal_succ, '成功', []);
        }
        $where = [['is_del', '=', 0]];
        if (!empty($name)) {
            $where[] = ['name', 'like', "%".trim($name)."%"];
        }
        $columns = [
            'id',
            'name'
        ];
        $data = Db::table('vote_show')
            ->select($columns)
            ->where($where)
            ->offset(0)
            ->limit(10)
            ->get();
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $data);
    }
    #场次列表
    #[RequestMapping(path: "sdlist", methods: "post")]
    public function sdlist()
    {
        $name = $this->request->input('name', '');
        $province_id = $this->request->input('province_id', 0);
        $city_id = $this->request->input('city_id', 0);
        $place_id = $this->request->input('place_id', 0);
        $is_realname = $this->request->input('is_realname', -1);
        $platform = $this->request->input('platform', 0);
        $status = $this->request->input('status', -1);
        $nograbdate = $this->request->input('nograbdate', 0);
        $currentPage = $this->request->input('currentPage', 1);
        $pageSize = $this->request->input('pageSize', 15);
        $where = [['is_del', '=', 0]];
        if (!empty($name)) {
            $where[] = ['name', 'like', "%".trim($name)."%"];
        }
        if (!empty($province_id)) {
            $where[] = ['province_id', '=', $province_id];
        }
        if (!empty($city_id)) {
            $where[] = ['city_id', '=', $city_id];
        }
        if (!empty($place_id)) {
            $where[] = ['place_id', '=', $place_id];
        }
        if ($is_realname!=-1) {
            $where[] = ['is_realname', '=', $is_realname];
        }
        if (!empty($platform)) {
            $where[] = ['platform', 'like', "%,".trim($platform).",%"];
        }
        if ($status!=-1) {
            $where[] = ['status', '=', $status];
        }
        $nograbdate_ids = [];
        if (!empty($nograbdate)) {
            $nograbdate_ids = Db::table('vote_showdate_detail')
                ->select(['showdate_id'])
                ->where([['grabtime', '=', 0]])
                ->groupBy('showdate_id')
                ->get()
                ->toArray();
            $nograbdate_ids = array_column($nograbdate_ids, "showdate_id");
        }
        $offset = ($currentPage - 1) * $pageSize;
        $columns = [
            'id',
            'city_id',
            'place_id',
            'name',
            'is_realname',
            'platform',
            'sort',
            'status',
            'created_at',
            'updated_at',
        ];
        $query = Db::table('vote_showdate')
            ->select($columns)
            ->where($where);
        if(!empty($nograbdate_ids)){
            $query->whereIn('id', $nograbdate_ids);
        }
        $total = $query->count();
        $datas = $query->offset($offset)
            ->limit($pageSize)
            ->orderBy('sort', 'desc')
            ->orderBy('created_at', 'desc')
            ->get()
            ->toArray();
        $return_list = [];
        if(!empty($datas)){
            $statusArr = ["1"=>'显示', "0"=>'隐藏'];
            $isRealnameArr = ["1"=>'实名', "0"=>'非实名'];
            foreach ($datas as $data) {
                $place_name = Db::table('place')
                    ->where(['id'=>$data['place_id']])
                    ->value("name");
                $sub_num = Db::table('vote_showdate_detail')
                    ->where(['showdate_id'=>$data['id']])
                    ->count();
                $city_name = Common::getCityById($data['city_id']);
                $status_ch = !empty($statusArr[$data['status']])?$statusArr[$data['status']]:"-";
                $is_realname_ch = !empty($isRealnameArr[$data['is_realname']])?$isRealnameArr[$data['is_realname']]:"-";
                $platform_ch = $this->platServ->getNameStrByIds($data['platform']);
                $return_list[] = [
                    'id'=>$data['id'],
                    'city_id'=>$data['city_id'],
                    'city_name'=>$city_name,
                    'place_id'=>$data['place_id'],
                    'place_name'=>$place_name,
                    'name'=>$data['name'],
                    'is_realname'=>$data['is_realname'],
                    'is_realname_ch'=>$is_realname_ch,
                    'platform'=>$data['platform'],
                    'platform_ch'=>$platform_ch,
                    'sort'=>$data['sort'],
                    'sub_num'=>!empty($sub_num)?$sub_num:0,
                    'status'=>$data['status'],
                    'status_ch'=>$status_ch,
                    'created_at'=>date("Y-m-d H:i:s", $data['created_at']),
                    'updated_at'=>date("Y-m-d H:i:s", $data['updated_at'])
                ];
            }
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', ['total'=>$total, 'list'=>$return_list]);
    }
    #场次详情
    #[RequestMapping(path: "sddetail", methods: "post")]
    public function sddetail()
    {
        $id = $this->request->input('id', 0);
        if(empty($id)){
            return $this->fmt_response(ReturnCode::para_miss,'ID不能为空');
        }
        $columns = [
            'id',
            'show_id',
            'province_id',
            'city_id',
            'place_id',
            'name',
            'is_realname',
            'platform',
            'level',
            'sort',
            'status',
            'created_at',
            'updated_at',
        ];
        $data = Db::table('vote_showdate')
            ->select($columns)
            ->where(['id'=>$id])
            ->first();
        $return_data = [];
        if(!empty($data)){
            $place_name = Db::table('place')
                ->where(['id'=>$data['place_id']])
                ->value("name");
            $show_name = Db::table('vote_show')
                ->where(['id'=>$data['show_id']])
                ->value("name");
            $province_name = Db::table('conf_provinces')
                ->where(['id'=>$data['province_id']])
                ->value("name");
            $city_name = Common::getCityById($data['city_id']);
            $status_ch = !empty($statusArr[$data['status']])?$statusArr[$data['status']]:"-";
            $is_realname_ch = !empty($isRealnameArr[$data['is_realname']])?$isRealnameArr[$data['is_realname']]:"-";
            $platform_ch = $this->platServ->getNameStrByIds($data['platform']);
            $exdate = $this->showServ->getShowdateDetail($data['id']);
            $return_data = [
                'id'=>$data['id'],
                'show_id'=>$data['show_id'],
                'show_name'=>$show_name,
                'province_id'=>$data['province_id'],
                'province_name'=>$province_name,
                'city_id'=>$data['city_id'],
                'city_name'=>$city_name,
                'place_id'=>$data['place_id'],
                'place_name'=>$place_name,
                'name'=>$data['name'],
                'is_realname'=>$data['is_realname'],
                'is_realname_ch'=>$is_realname_ch,
                'platform'=>$data['platform'],
                'platform_array'=>array_values(array_filter(explode(",", $data['platform']))),
                'platform_ch'=>$platform_ch,
                'exdate'=>$exdate,
                'level'=>json_decode($data['level'], true),
                'sort'=>$data['sort'],
                'status'=>$data['status'],
                'status_ch'=>$status_ch,
                'created_at'=>date("Y-m-d H:i:s", $data['created_at']),
                'updated_at'=>date("Y-m-d H:i:s", $data['updated_at'])
            ];
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $return_data);
    }
    #场次保存
    #[RequestMapping(path: "sdsave", methods: "post")]
    public function sdsave()
    {
        $id = $this->request->input('id', 0);
        $show_id = $this->request->input('show_id', 0);
        $province_id = $this->request->input('province_id', 0);
        $city_id = $this->request->input('city_id', 0);
        $place_id = $this->request->input('place_id', 0);
        $name = $this->request->input('name', '');
        $is_realname = $this->request->input('is_realname', -1);
        $platform = $this->request->input('platform', '');
        $exdate = $this->request->input('exdate', '');
        $level = $this->request->input('level', '');
        $sort = $this->request->input('sort', 0);
        $status = $this->request->input('status', -1);
        if(empty($show_id)){
            return $this->fmt_response(ReturnCode::para_miss,'未关联演出系列');
        }
        if(empty($province_id) || empty($city_id)){
            return $this->fmt_response(ReturnCode::para_miss,'未关联省市');
        }
        if(empty($place_id)){
            return $this->fmt_response(ReturnCode::para_miss,'场馆不能为空');
        }
        if(empty($name)){
            return $this->fmt_response(ReturnCode::para_miss,'名称不能为空');
        }
        if(empty($exdate)){
            return $this->fmt_response(ReturnCode::para_miss,'时间参数错误');
        }
        $ft_exdate = $this->showServ->formatExdate($exdate);
        if(empty($ft_exdate['actdate'])){
            return $this->fmt_response(ReturnCode::para_miss,'演出时间错误');
        }
        if(empty($level)){
            return $this->fmt_response(ReturnCode::para_miss,'票档不能为空');
        }
        if($status==-1){
            return $this->fmt_response(ReturnCode::para_miss,'状态错误');
        }
        $person_id = Db::table('vote_show')
            ->where(['id'=>$show_id])
            ->value("person_id");
        $params = [
            'id'=>$id,
            'person_id'=>$person_id,
            'show_id'=>$show_id,
            'province_id'=>$province_id,
            'city_id'=>$city_id,
            'place_id'=>$place_id,
            'name'=>$name,
            'is_realname'=>$is_realname,
            'platform'=>$platform,
            'ft_exdate'=>$ft_exdate,
            'level'=>$level,
            'sort'=>$sort,
            'status'=>$status
        ];
        $res = $this->showServ->showdateSave($params);
        if(!$res){
            return $this->fmt_response(ReturnCode::deal_false,'失败');
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #场次删除
    #[RequestMapping(path: "sddel", methods: "post")]
    public function sddel()
    {
        $id = $this->request->input('id', 0);
        if(empty($id)){
            return $this->fmt_response(ReturnCode::para_miss,'ID不能为空');
        }
        $res = Db::table('vote_showdate')
            ->where(['id'=>$id])
            ->update(['is_del'=>1]);
        if(!$res){
            return $this->fmt_response(ReturnCode::deal_false,'失败');
        }
        // $this->showServ->synDelSubBySdId($id);
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
}